nanopyx.core.transform.interpolation_fft_zoom

 1import numpy as np
 2
 3
 4def magnify(
 5    image: np.ndarray,
 6    magnification: float = 2,
 7    enforce_same_value: bool = True,
 8) -> np.ndarray:
 9    """
10    Zoom an image by zero-padding its Discrete Fourier transform
11    :param image: 2D grid of pixel values
12    :param magnification: factor by which to multiply the dimensions of the image
13    :param enforce_same_value: if True, the value of the original samples will be preserved
14    :return: zoomed image
15
16    REF: based on https://github.com/centreborelli/fourier
17    """
18    rows, cols = image.shape
19
20    # Fourier transform with the zero-frequency component at the center
21    imageFt = np.fft.fftshift(np.fft.fft2(image))
22
23    # the zoom-in is performed by zero padding the Fourier transform
24    rowsM = rows * magnification
25    colsM = cols * magnification
26    r0 = rowsM // 2 - rows // 2
27    c0 = colsM // 2 - cols // 2
28    imageFtPadded = np.zeros((rowsM, colsM), dtype=np.complex64)
29    imageFtPadded[r0 : r0 + rows, c0 : c0 + cols] = imageFt
30
31    # apply ifftshift before taking the inverse Fourier transform
32    imageM = np.fft.ifft2(np.fft.ifftshift(imageFtPadded))
33
34    # if the input is a real-valued image, then keep only the real part
35    if np.isrealobj(image):
36        imageM = np.real(imageM)
37
38    # to preserve the values of the original samples, the L2 norm has to by multiplied by magnification*magnification
39    imageM *= magnification * magnification
40
41    if enforce_same_value:
42        imageM[::magnification, ::magnification] = image
43
44    # return the image casted to the input data type
45    return imageM.astype(image.dtype, copy=False)
def magnify( image: numpy.ndarray, magnification: float = 2, enforce_same_value: bool = True) -> numpy.ndarray:
 5def magnify(
 6    image: np.ndarray,
 7    magnification: float = 2,
 8    enforce_same_value: bool = True,
 9) -> np.ndarray:
10    """
11    Zoom an image by zero-padding its Discrete Fourier transform
12    :param image: 2D grid of pixel values
13    :param magnification: factor by which to multiply the dimensions of the image
14    :param enforce_same_value: if True, the value of the original samples will be preserved
15    :return: zoomed image
16
17    REF: based on https://github.com/centreborelli/fourier
18    """
19    rows, cols = image.shape
20
21    # Fourier transform with the zero-frequency component at the center
22    imageFt = np.fft.fftshift(np.fft.fft2(image))
23
24    # the zoom-in is performed by zero padding the Fourier transform
25    rowsM = rows * magnification
26    colsM = cols * magnification
27    r0 = rowsM // 2 - rows // 2
28    c0 = colsM // 2 - cols // 2
29    imageFtPadded = np.zeros((rowsM, colsM), dtype=np.complex64)
30    imageFtPadded[r0 : r0 + rows, c0 : c0 + cols] = imageFt
31
32    # apply ifftshift before taking the inverse Fourier transform
33    imageM = np.fft.ifft2(np.fft.ifftshift(imageFtPadded))
34
35    # if the input is a real-valued image, then keep only the real part
36    if np.isrealobj(image):
37        imageM = np.real(imageM)
38
39    # to preserve the values of the original samples, the L2 norm has to by multiplied by magnification*magnification
40    imageM *= magnification * magnification
41
42    if enforce_same_value:
43        imageM[::magnification, ::magnification] = image
44
45    # return the image casted to the input data type
46    return imageM.astype(image.dtype, copy=False)

Zoom an image by zero-padding its Discrete Fourier transform

Parameters
  • image: 2D grid of pixel values
  • magnification: factor by which to multiply the dimensions of the image
  • enforce_same_value: if True, the value of the original samples will be preserved
Returns

zoomed image

REF: based on https://github.com/centreborelli/fourier